package ro.gateway.aida.obj.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import ro.gateway.aida.db.DBPersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.ActivityRelation;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: ro-gateway</p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: ActivityRelationDB.java,v 1.1 2004/10/24 23:37:11 mihaipostelnicu Exp $
 */
public class ActivityRelationDB extends DBPersistenceManager
{
      private ActivityRelationDB( PersistenceToken token )
      {
            super( token );
      }

      public static ActivityRelationDB getManager( PersistenceToken token )
      {
            return new ActivityRelationDB( token );
      }

      public void insert(
            ActivityRelation[] items )
            throws SQLException
      {

            if ( items == null ) return;

            for ( int i = 0; i < items.length; i++ )
            {
                  insert( items[ i ] );
            }
      }

      private void insert(
            ActivityRelation item )
            throws SQLException
      {

            Connection con = getConnection();

            try
            {
                  PreparedStatement ps = con.prepareStatement( INSERT );
                  ps.setLong( 1, item.getActivity_id() );
                  ps.setLong( 2, item.getRelto_activity_id() );
                  ps.setString( 3, item.getRelation() );
                  ps.executeUpdate();
                  ps.close();
            }
            catch ( SQLException sqlEx )
            {
                  throw sqlEx;
            }
            finally
            {
                  con.close();
            }

            return;
      }

      public void delete_for_activity(
            long id )
            throws SQLException
      {

            Connection con = getConnection();

            try
            {
                  PreparedStatement ps = con.prepareStatement( DELETE_FOR_ACTIVITY );
                  ps.setLong( 1, id );
                  ps.setLong( 2, id );
                  ps.executeUpdate();
                  ps.close();
            }
            catch ( SQLException sqlEx )
            {
                  throw sqlEx;
            }
            finally
            {
                  con.close();
            }
      }

      public ActivityRelation[] getForActivity(
            long activity_id )
            throws SQLException
      {

            Connection con = getConnection();
            ActivityRelation[] result = null;
            ArrayList items = new ArrayList();

            try
            {
                  PreparedStatement ps = con.prepareStatement( GET_FOR_ACTIVITY );

                  ps.setLong( 1, activity_id );
                  ResultSet rs = ps.executeQuery();
                  while ( rs.next() )
                  {
                        ActivityRelation item = new ActivityRelation();

                        item.setActivity_id( activity_id );
                        item.setRelto_activity_id( rs.getLong( 1 ) );
                        item.setRelation( rs.getString( 2 ) );

                        items.add( item );
                  }
                  rs.close();

                  ps.close();
            }
            catch ( SQLException sqlEx )
            {
                  throw sqlEx;
            }
            finally
            {
                  con.close();
            }

            if ( items.size() > 0 )
            {
                  result = new ActivityRelation[ items.size() ];
                  items.toArray( result );

                  for ( int i = 0; i < result.length; i++ )
                  {
                        result[ i ].setRelToTitle( GenericActivityPropertyDB.getManager( token ).
                              getFirstForActivity( "gen_titles", result[ i ].getRelto_activity_id() ) );
//                        System.out.println( "am tras din db pt:" + result[ i ].getRelto_activity_id() + ":" + result[ i ].getRelToTitle() );
                  }
            }

            return result;
      }

      private static final String DELETE_FOR_ACTIVITY =
            "DELETE FROM act_relations WHERE act_id=? OR relatedTo_id=?";

      private static final String INSERT =
            "INSERT INTO act_relations (act_id, relatedTo_id, relation) " +
            "VALUES (?,?,?)";

      private static final String GET_FOR_ACTIVITY =
            "SELECT relatedTo_id, relation " +
            "FROM act_relations WHERE act_id=?";

}
